package com.bocai.grainshop.order.interceptor;

import com.bocai.common.constant.AuthServerConstant;
import com.bocai.common.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class WebInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberVo> loginUser = new ThreadLocal<>();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //通过路径匹配放行 ware服务发来的远程调用
        boolean match = new AntPathMatcher().match("/order/order/OrderSn/**", request.getRequestURI());
        if(match){
            return true;
        }

        Object attribute = request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if(attribute!=null){
            MemberVo memberVo = (MemberVo) attribute;
            loginUser.set(memberVo);
            return true;
        }else {
            request.getSession().setAttribute("msg","请先登录");
            response.sendRedirect("http://auth.grainshop.com/login");
            return false;
        }
    }
}
